package com.snoopy.gas1.service.impl;

import com.snoopy.gas1.entity.Employee_info;
import com.snoopy.gas1.mapper.Employee_infoMapper;
import com.snoopy.gas1.service.Employee_infoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

@Service
public class Employee_infoServiceImpl implements Employee_infoService {

    @Autowired
    private Employee_infoMapper employeeInfoMapper;

    @Override
    public List<Employee_info> lists() {
        return employeeInfoMapper.lists();
    }

    @Override
    public List<Employee_info> searchByName(String name) {
        if (name == null || name.trim().isEmpty()) {
            return lists(); // 空查询时返回所有
        }
        return employeeInfoMapper.searchByName(name);
    }

    @Override
    public void deleteById(int id) {
        if (id > 0) {
            employeeInfoMapper.deleteById(id);
        }
    }

    @Override
    public int add(Employee_info employeeInfo) {
        // 数据校验
        if (employeeInfo.getEmployeeName() == null || employeeInfo.getEmployeeName().trim().isEmpty()) {
            throw new RuntimeException("员工姓名不能为空");
        }
        if (employeeInfo.getHireDate() == null) {
            throw new RuntimeException("入职日期不能为空");
        }
        // 设置默认值
        if (employeeInfo.getWorkStatus() == null) {
            employeeInfo.setWorkStatus(1); // 默认在职
        }
        employeeInfo.setUpdateTime(new Date()); // 设置当前时间为更新时间
        return employeeInfoMapper.add(employeeInfo);
    }

    @Override
    public Employee_info getById(int id) {
        return employeeInfoMapper.getById(id);
    }

    @Override
    public int update(Employee_info employeeInfo) {
        // 数据校验
        if (employeeInfo.getEmployeeName() == null || employeeInfo.getEmployeeName().trim().isEmpty()) {
            throw new RuntimeException("员工姓名不能为空");
        }
        if (employeeInfo.getHireDate() == null) {
            throw new RuntimeException("入职日期不能为空");
        }
        employeeInfo.setUpdateTime(new Date()); // 更新时间戳
        return employeeInfoMapper.update(employeeInfo);
    }
}